package apiwork.lcbmi.action;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlException;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import apiwork.bean.ISessionBean;
import apiwork.lcbmi.protocaladaptor.IsppProtocolAdaptor;
import apiwork.lcbmi.util.config.LcbmiMainConfig;
import apiwork.mina.httpserver.HttpResponseMessage;
import apiwork.mina.httpserver.stream.HttpStreamServer;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

@SuppressWarnings("serial")
public class IsppMainAction extends ActionSupport{
	private static Logger logger=Logger.getLogger(IsppMainAction.class);
	String datamessage = null;
	
	public String startup() {
		logger.info("datamessage in isppmainaction:"+datamessage);
		ActionContext context = ActionContext.getContext();
		Map paramsMap = (Map) context.get(ActionContext.PARAMETERS);
		IsppProtocolAdaptor adaptor = new IsppProtocolAdaptor();
		adaptor.operationClassify(paramsMap);
		logger.info("paramsMap:"+paramsMap);
		try {
			String opt=(String)Ognl.getValue("commandMap.operator", paramsMap);
			logger.info("commandMap.operator="+opt);
			return opt;
		} catch (OgnlException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void protocalAnalyseError() {
		logger.info("ISPP protocal analyse error:"+datamessage);
	}
	
	public void notSupport() {
		logger.info("datamessage in not support:"+datamessage);
	}
	
	public void validError() {
		logger.info("validError in isppmain");
		logger.info(getActionErrors());
//		logger.info(getActionMessages());
		logger.info(getFieldErrors());
	}
	
	public void actionFinish() {
		logger.info("actionFinish in isppmain");
//		logger.info(getActionErrors());
//		logger.info(getFieldErrors());
		try {
			String charset = "GB18030";//GB18030
			ActionContext actionContext = ActionContext.getContext();
			ValueStack stack = actionContext.getValueStack();
			String process_result = stack.findString("process_result"); 
			String process_result_message = stack.findString("process_result_message");
//			process_result_message=new String(process_result_message.getBytes("iso-8859-1"), charset);//zhaigx add
			logger.info("http response result=["+process_result+"]"+process_result_message);
			IoSession session = (IoSession)new ISessionBean().getSession();
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter((OutputStream) session.getAttribute("outputstream"),Charset.forName(charset))));
			HttpResponseMessage reponse = new HttpResponseMessage();
			reponse.setServer("IsppServer Version 1.1");
			reponse.setContentType("text/xml;charset="+charset);//zhaig add:charset
			String body = "";
			body+="<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n";
				body+="	<SOAP-ENV:Header>\n";
				body+="		<m:Username xmlns:m=\"http://ispp.com.cn/ispp_npi/\">String</m:Username>\n";
				body+="		<m:Password xmlns:m=\"http://ispp.com.cn/ispp_npi/\">String</m:Password>\n";
				body+="		<m:MessageID xmlns:m=\"http://ispp.com.cn/ispp_npi/\">String</m:MessageID>\n";
				body+="		<m:Address xmlns:m=\"http://ispp.com.cn/ispp_npi/\">String</m:Address>\n";
				body+="	</SOAP-ENV:Header>\n";
				body+="	<SOAP-ENV:Body>\n";
				body+="		<m:root xmlns:m=\"http://ispp.com.cn/ispp_npi/\">\n";
				body+="			<msg_head>\n";
				body+="				<time>String</time>\n";
				body+="				<from>String</from>\n";
				body+="				<to>String</to>\n";
				body+="				<msg_type>String</msg_type>\n";
				body+="				<serial>String</serial>\n";
				body+="			</msg_head>\n";
				body+="			<interface_msg>\n";
				body+="				<msg_response>\n";
				body+="					<ResponseClass Name=\"Response\">\n";
				body+="						<ResponseSubClass Name=\"CreateMOResponse\">\n";
				body+="							<ResultCode>"+process_result+"</ResultCode>\n";
				body+="							<ResultDescr>"+URLEncoder.encode((process_result_message),charset)+"</ResultDescr>\n";//zhaigx: URLEncoder.encode((process_result_message),charset)
				body+="						</ResponseSubClass>\n";
				body+="					</ResponseClass>\n";
				body+="				</msg_response>\n";
				body+="			</interface_msg>\n";
				body+="		</m:root>\n";
				body+="	</SOAP-ENV:Body>\n";
				body+="</SOAP-ENV:Envelope>\n";
			reponse.appendBody(body);
			out.println(reponse.encode(Charset.forName(charset).newEncoder()).getString(Charset.forName(charset).newDecoder()));						
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getDatamessage() {
		return datamessage;
	}

	public void setDatamessage(String datamessage) {
		this.datamessage = datamessage;
	}
	
	
}
